/**
 * Created by 121 on 2016/3/22.
 */
/**
 * Created by Administrator on 2016/1/26.
 */

require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function ($, common, ready, template, Collection, Model, dialog) {
    var index = function () {
        this.init();
    }
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            //this.event();
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'server/update',
                condition: {
                    limit: 10,
                    offset: 0
                },
                model: {
                    tag_name: 'tr',
                    url: _global.url.api + '',
                    event: {
                        '.rollback:click': 'rollback',
                        '.restore:click': 'restore'
                    },

                    rollback: function (e, dom) {
                        e.preventDefault();
                        var sha = dom.attr('attr');
                        var api_key = dom.attr('git-type');
                        dialog({
                            content: '确定回滚快照吗？',
                            modal: true,
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'server/rollback',
                                        data: {'sha': sha, 'api_key': api_key}
                                    }).put()) {
                                }
                                $('.rollback' + sha).html('已回滚,请点击恢复');
                                $('.rollback' + sha).attr('disabled', 'disabled');
                                loaded();
                            },
                            cancel: function () {
                            }
                        }).show(e.target);
                    },
                    restore: function (e, dom) {
                        e.preventDefault();
                        var sha = dom.attr('attr');
                        var shas = dom.attr('attrs');
                        var api_key = dom.attr('git-type');
                        dialog({
                            content: '确定恢复快照存储吗？',
                            modal: true,
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'server/rollback',
                                        data: {'sha': sha, 'api_key': api_key}
                                    }).put()) {
                                }
                                $('.restore' + shas).html('已恢复');
                                $('.restore' + shas).attr('disabled', 'disabled');
                                $('.rollback' + shas).removeAttr('disabled');
                                $('.rollback' + shas).html('回滚快照');
                                loaded();
                            },
                            cancel: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        }
    }
    new index();
});
